package main

import (
	"fmt"
	"net"
	"time"
)

func main() {
	conn, err := net.Dial("tcp", ":8888") //拨号
	if err != nil {
		fmt.Println("et.Dial err:", err)
		return
	}

	fmt.Println("client与server连接建立成功!")

	sendData := []byte("helloworld")

	for {
		cnt, err := conn.Write(sendData)
		if err != nil {
			fmt.Println("conn.Write err: ", err)
			return
		}

		fmt.Println("Client to Server cnt: ", cnt, ",data: ", string(sendData))

		//接收服务器返回的数据
		//创建buf用于接收服务器返回的数据
		buf := make([]byte, 1024)

		cnt, err = conn.Read(buf)
		if err != nil {
			fmt.Println("conn.Read err: ", err)
			return
		}

		fmt.Println("client receiver from server,cnt: ", cnt, ",data: ", string(buf[:cnt]))
		time.Sleep(time.Second)
	}

	conn.Close()
}
